package com.asa.chart.suanming;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;

import com.asa.chart.base.Line;
import com.asa.chart.work.AlgoVisHelper;


/**
 * 
 * @author asa
 *
 */
public class AlgoFrame extends JFrame{
	
	public AlgoFrame(String title,int canvasWidth,int canvasHeight) {
		super(title);
		this.canvasWidth = canvasWidth;
		this.canvasHeight = canvasHeight;
		
		AlgoCanvas canvas = new AlgoCanvas();
//		canvas.setPreferredSize(new Dimension(canvasWidth,canvasHeight));
		
//		this.setSize(canvasWidth,canvasHeight);
		this.setContentPane(canvas);
		pack();
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		
	}
	public int getCanvasWidth() {
		return canvasWidth;
	}
	public int getCanvasHeight() {
		return canvasHeight;
	}
	private int canvasWidth;
	private int canvasHeight;

	private List<Line> mapline;
	List<BlockString> wenzi;
	
	public void render(List<Line> mapline,List<BlockString> wenzi) {
		
		this.mapline = mapline;		
		this.wenzi = wenzi;		

		this.repaint();
	}
	
	private class AlgoCanvas extends JPanel{
		
		@Override
		public void paintComponent(Graphics g) {
			
			super.paintComponent(g);
			
			Graphics2D g2d = (Graphics2D) g;
			
			AlgoVisHelper.setColor(g2d,Color.GRAY);
			if (mapline!=null) {
				for (int i = 0; i < mapline.size(); i++) {
					Line line = mapline.get(i);
					int x1 = line.spoint.x;
					int y1 = line.spoint.y;
					int x2 = line.epoint.x;
					int y2 = line.epoint.y;
					AlgoVisHelper.drawLine(g2d, x1, y1, x2, y2);
				
				}
			}
			
//			AlgoVisHelper.setStrokeWidth(g2d, 5);
//			AlgoVisHelper.setColor(g2d,Color.RED);
//			AlgoVisHelper.strokeReactangle(g2d, 50, 50, 60, 60);
			
			
			
			
			RenderingHints hints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
			g2d.addRenderingHints(hints);

			
			AlgoVisHelper.setStrokeWidth(g2d, 5);

			

			
			AlgoVisHelper.setColor(g2d,Color.RED);

			if (wenzi!=null) {
				for (int i = 0; i < wenzi.size(); i++) {
					BlockString blockString = wenzi.get(i);
					AlgoVisHelper.drawString(g2d, blockString.content, blockString.x, blockString.y);

				}
			}

			
			
			
			
			AlgoVisHelper.drawString(g2d, AlgoVisualizer.midTime+"", canvasWidth-10, canvasHeight-10);

			
			
			
			
			
			
		}
		
		
		@Override
		public Dimension getPreferredSize() {
			
			return new Dimension(canvasWidth, canvasHeight);
		}
		
	}
	
	
	
}
